var globalAdvData = [{
	"id": "ad-all-top",
	"type": "normal",
	"width": "100%",
	"height": "auto",
	"title": "",
	"hotmapimg": "",
	"el": [{
		"img": "http:\/\/d1.jiuyan.info\/2013\/12\/13\/B8CECB16-0AFB-5DE3-BC38-712ED7735176.jpg?v=2",
		"url": "http:\/\/huodong.itugo.com\/1212",
		"start_at": 1386864000,
		"end_at": 1386950400
	}]
}];
var __STATIC__ = 'http://res.jiuyan.info/201312131111';
var banerResUrl = 'http://itugo.com/api/getad';
var jquery = __STATIC__ + '/lib/jquery/jquery-sea-fake';
var tabsl = __STATIC__ + '/lib/requiretabs/1.0.0.6/tabs';
var mustache = __STATIC__ + '/lib/mustache/0.7.2/mustache';
var aop = __STATIC__ + '/lib/jquery-aop/1.3/aop.pack';
define([jquery, mustache, aop, tabsl, './adv_config'],
function(require, exports, module) {
	var Mustache = require(mustache);
	require(aop);
	var advConfig = require('./adv_config');
	function cookie(key, value, options) {
		if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
			options = $.extend({},
			options);
			if (value === null || value === undefined) {
				options.expires = -1;
			}
			if (typeof options.expires === 'number') {
				var days = options.expires,
				t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}
			value = String(value);
			return (document.cookie = [encodeURIComponent(key), '=', options.raw ? value: encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path: '', options.domain ? '; domain=' + options.domain: '', options.secure ? '; secure': ''].join(''));
		}
		options = value || {};
		var decode = options.raw ?
		function(s) {
			return s;
		}: decodeURIComponent;
		var pairs = document.cookie.split('; ');
		for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
			if (decode(pair[0]) === key) return decode(pair[1] || '');
		}
		return null;
	};
	function init_online() {
		var ids = '';
		$.each(globalAdvData,
		function(i) {
			ids = globalAdvData[i].id;
			$("[banner-id='" + ids + "']").each(function(k) {
				switch (globalAdvData[i].type) {
				case 'sink':
					init_sink.call(this, globalAdvData[i]);
					break;
				case 'carousel':
					init_carousel.call(this, globalAdvData[i]);
					break;
				case 'hotmap':
					init_hotmap.call(this, globalAdvData[i]);
					break;
				default:
					init_normal.call(this, globalAdvData[i]);
					break;
				}
			});
		});
	}
	function req(option) {
		var dtd = $.Deferred();
		var defaults = {
			type: 'GET',
			dataType: 'json'
		};
		var options = $.extend({},
		defaults, option);
		var jqXHR = $.ajax(options).done(function(msg) {
			if (msg && (typeof msg.succ == 'boolean')) {
				dtd.resolve(msg, jqXHR);
			} else {
				dtd.reject(jqXHR, 'parsererror');
			}
		}).fail(function(jqXHR, textStatus) {
			if (jqXHR.status == 401) {
				login();
			}
			dtd.reject(jqXHR, textStatus);
		});
		return dtd.promise();
	}
	function init_pre(ids) {
		var data = "",
		_this = $("[banner-id='" + ids + "']");
		_this.each(function(i) {
			req({
				url: banerResUrl,
				type: 'GET',
				dataType: 'json',
				data: {
					ids: ids
				}
			}).done(function(res) {
				if (res.succ) {
					data = $.parseJSON(res.data) || '';
					switch (data.type) {
					case 'sink':
						init_sink.call(_this.get(0), data);
						break;
					case 'carousel':
						init_carousel.call(_this.get(0), data);
						break;
					case 'hotmap':
						init_hotmap.call(_this.get(0), data);
						break;
					default:
						init_normal.call(_this.get(0), data);
						break;
					}
				}
			});
		});
	}
	function init_template() {
		var $ts = $(this),
		data = arguments[0] || {},
		html = "";
		html = Mustache.to_html(arguments[arguments.length - 1], data);
		$ts.html(html);
		return html;
	}
	function init_sink() {
		var $ts = $(this),
		data = arguments[0] || {},
		myName = "";
		init_template.apply(this, [].concat(data, advConfig.sink));
		myName = cookie("banner" + data.id);
		var banner_small = $ts.find(".banner_small");
		var bannerDown = $ts.find(".bannerDown");
		if (!myName) {
			bannerDown.slideDown(600);
			setTimeout(function() {
				bannerDown.slideUp(600,
				function() {
					banner_small.slideDown(600);
				});
			},
			5000);
		} else {
			banner_small.show();
		}
		cookie("banner" + data.id, 1);
	}
	function init_carousel() {
		var requiretabs = require(tabsl);
		init_template.apply(this, [].concat(arguments[0], advConfig.carousel));
		var $licon = $(this).find('.ad-banner-licon');
		$licon.css('marginLeft', -($licon.width() / 2));
		$(this).tabs({
			triggerDom: ".tabs-trgDoms",
			contentDom: ".tabs-cotDoms",
			auto: true,
			effect: "window8flows",
			event: "mouseenter",
			delay: 6000
		});
	}
	function init_hotmap() {
		init_template.apply(this, [].concat(arguments[0], advConfig.hotmap));
	}
	function init_normal() {
		init_template.apply(this, [].concat(arguments[0], advConfig.normal));
	}
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}
	return {
		init: function(a) {
			$(function() {
				var ids = getQueryString('adpreview');
				if (ids) {
					init_pre(ids);
				}
				 else {
					init_online();
				}
			});
		},
		sink: init_sink,
		carousel: init_carousel,
		hotmap: init_hotmap,
		normal: init_normal
	}
});